package com.zmn.plat.admin.controller.service.item.servicetime;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.model.entity.service.item.servicetime.ServServiceTime;
import com.zmn.plat.model.entity.service.item.servicetime.ServServiceTimeQuery;
import com.zmn.plat.services.interfaces.service.item.servicetime.ServServiceTimeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 项目服务时长基础修改Controller
 *
 * @author yangshuangping
 * @since 2021/07/27 10:36
 */
@Slf4j
@RestController
@RequestMapping("/time/servicetime")
public class ServServiceTimeModifyController extends PlatBaseController {

    @Resource
    private ServServiceTimeService servServiceTimeService;

    /**
     * 描述: 保存
     *
     * @author yangshuangping
     * @since 2021/07/27 10:36
     */
    @RequestMapping("/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ServServiceTime servServiceTime) {

        Date now = DateUtil.getNow();
        ServServiceTimeQuery query = new ServServiceTimeQuery();
        query.setItemId(servServiceTime.getItemId());
        query.setCompanyId(servServiceTime.getCompanyId());
        ServServiceTime newServiceTime = BeanMapper.map(servServiceTime, ServServiceTime.class);
        try {
            List<ServServiceTime> timeList = servServiceTimeService.listByQuery(query);
            if (CollectionUtil.isNotNullOrEmpty(timeList)) {
                newServiceTime = timeList.get(0);
            }
            Integer serviceTimeId = newServiceTime.getServiceTimeId();
            newServiceTime.setUpdater(getMcStaffRealName());
            newServiceTime.setUpdateTime(now);
            newServiceTime.setServiceMinutes(servServiceTime.getServiceMinutes());
            if (NumberUtil.isNullOrZero(serviceTimeId)) {
                newServiceTime.setCreater(getMcStaffRealName());
                newServiceTime.setCreateTime(now);
                servServiceTimeService.insert(newServiceTime);
            } else {
                servServiceTimeService.updateByKey(newServiceTime);
            }
            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 描述: 修改状态
     *
     * @param ids    id集合
     * @param status 状态
     * @author yangshuangping
     * @since 2021/07/27 10:36
     */
    @RequestMapping(value = "/updateStatus")
    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "ids") String ids,
                                             @RequestParam(value = "status") Integer status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        try {
            servServiceTimeService.updateStatusBatch(arrayIds, status, getMcStaffRealName());
            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            log.error("{},{}", e.getMessage(), e);
            return ResponseDTO.fail("修改状态失败");
        }
    }

    /**
     * 描述: 删除
     *
     * @param serviceTimeId 主键
     * @author yangshuangping
     * @since 2021/07/27 10:36
     */
    @RequestMapping(value = "/delete")
    public ResponseDTO delete(@RequestParam(value = "serviceTimeId") Integer serviceTimeId) {

        try {
            if (NumberUtil.isNullOrZero(serviceTimeId)) {
                return ResponseDTO.fail("请选择要删除的数据！");
            }
            servServiceTimeService.deleteByKey(serviceTimeId);
            return ResponseDTO.success("删除成功！");
        } catch (Exception e) {
            log.error("删除失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

}
